package com.shenghua.filter;

import com.shenghua.dao.model.User;
import com.shenghua.utils.StringUtils;
import com.shenghua.utils.constants.UserStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 页面访问过滤器
 * Created by Administrator on 2019/1/1.
 */
@Component
public class LoginFilter implements Filter {
    private static Logger logger = LoggerFactory.getLogger(LoginFilter.class);
    private List<String> allowUrls; // 允许的路径

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        allowUrls = new ArrayList<>();
        allowUrls.add("/index");
        allowUrls.add("/index/mobile");
        allowUrls.add("/index/normal");
        allowUrls.add("/user/loginSys");
        allowUrls.add("/user/regUser");
        allowUrls.add("/user/exitSys");
        allowUrls.add("/user/checkLinkman");
        allowUrls.add("/user/queryUser");
        allowUrls.add("/user/selectUserByPage");
        allowUrls.add("/favicon.ico");
    }

    @Override
    public void doFilter(ServletRequest servletRequest,
                         ServletResponse servletResponse,
                         FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        servletResponse.setContentType("text/html; charset=utf-8");
        String url=null;
        // 判断不需要过滤的路径
        if (null != allowUrls && allowUrls.size() > 0) {
            url = request.getRequestURI();
            String requestUrl = request.getRequestURI().replace(request.getContextPath(), "");
            logger.info("[LoginFilter] URI={}", requestUrl +  " \t" + url);
            if (requestUrl.startsWith("/<script>")) {
                servletResponse.getWriter().println(requestUrl.substring(1) + "?" + request.getQueryString());
                return;
            }
            if(allowUrls.contains(requestUrl)||
                    requestUrl.startsWith("/sh/css") ||
                    requestUrl.startsWith("/sh/js") ||
                    requestUrl.startsWith("/sh/themes") ||
                    requestUrl.startsWith("/sh/upload") ||
                    requestUrl.startsWith("/sh/temp")){
                filterChain.doFilter(request, response);
                return;
            }
        }

        Object userSession = request.getSession().getAttribute("userSession");
        if(null == userSession || "".equals(userSession)) {
            logger.info("------------未登录--------------");
            servletResponse.getWriter().println("<script>window.top.location.href =\"/index\"</script>");
            return;
        }
        User user = (User)userSession;
        if(null == user || StringUtils.isBlank(user.getStatus()) ||
                UserStatus.STOPED.getVal().equals(user.getStatus()) ||
                UserStatus.FORBIDDEN.getVal().equals(user.getStatus()) ||
                UserStatus.FROZEN.getVal().equals(user.getStatus())){
            logger.info("------------账号状态为："+user.getStatus()+"--------------");
            servletResponse.getWriter().println("<script>window.top.location.href =\"/index\"</script>");
            return;
        }
        filterChain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
